var xmlRequest = null;

// new a XMLHttpRequest
function newXMLHttpRequest() {
	if (window.ActiveXObject) {//IE Browser
		try {
			xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			try {
				xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				alert("Can't create XMLHttpRequest!");
			}
		}
	} else if (window.XMLHttpRequest) {//Others Browser
		xmlRequest = new XMLHttpRequest();
		//		if(xmlRequest.overrideMimeType){
		//			xmlRequest.overrideMimeType('text/xml');
		//		}
	}
	return xmlRequest;
}

function doGetRequest(url, queryString, isAsync, processFunc) {
	if (xmlRequest == null) {
		newXMLHttpRequest();
	}
	if (queryString != null && queryString.length > 0) {
		url = url + "?" + queryString;
	}
	xmlRequest.open("GET", url, isAsync);
	//Note:assign the function name,must without ().
	//xmlRequest.onreadystatechange = handleStateChange;
	xmlRequest.onreadystatechange = function() {
		if (xmlRequest.readyState == 4) {
			if (xmlRequest.status == 200) {
				if (xmlRequest.getResponseHeader("Content-Type") == "text/plain;charset=utf-8") {
					processFunc(xmlRequest.responseText);
				} else {
					processFunc(xmlRequest.responseXML);
				}
			}
		}
	};
	xmlRequest.send(null);
}

function doPostRequest(url, queryString, isAsync, requestBody, processFunc) {
	if (xmlRequest == null) {
		newXMLHttpRequest();
	}
	if (queryString != null && queryString.length > 0) {
		url = url + "?" + queryString;
	}
	xmlRequest.open("POST", url, isAsync);
	xmlRequest.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	//Note:assign the function name,must without ().
	//xmlRequest.onreadystatechange = handleStateChange;
	xmlRequest.onreadystatechange = function() {
		if (xmlRequest.readyState == 4) {
			if (xmlRequest.status == 200) {
				if (xmlRequest.getResponseHeader("Content-Type") == "text/plain;charset=utf-8") {
					processFunc(xmlRequest.responseText);
				} else {
					processFunc(xmlRequest.responseXML);
				}
			}
		}
	};
	xmlRequest.send(requestBody);
}

function handleStateChange() {
	if (xmlRequest.readyState == 4) {
		if (xmlRequest.status == 200) {
			if (xmlRequest.responseText != "ok") {
				document.getElementById('alertText').innerHTML = xmlRequest.responseText;
			} else {
				document.getElementById('alertText').innerHTML = "";
			}
		}
	}
}
